package com.iretailer.report.service;

import com.googlecode.htmlcompressor.compressor.HtmlCompressor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;
import java.util.Objects;

/**
 * @program: report
 * @description:
 * @author: Bennie
 * @create: 2020-06-12 09:41
 **/

@Slf4j
@Service
public class MailService {

    @Value("${spring.mail.username}")
    private String sender;

    private final JavaMailSender mailSender;

    private final TemplateEngine templateEngine;

    private final HtmlCompressor htmlCompressor = new HtmlCompressor();


    public MailService(JavaMailSender mailSender, TemplateEngine templateEngine) {
        this.mailSender = mailSender;
        this.templateEngine = templateEngine;
        htmlCompressor.setCompressCss(true);
        htmlCompressor.setCompressJavaScript(true);
    }

    /**
     * 发送邮件
     *
     * @param to        邮件接收者
     * @param subject   邮件主题
     * @param content   邮件内容
     * @param html      是否为html格式
     * @param filePaths 附件路径列表
     * @throws MessagingException 邮件发送异常
     */
    public String send(String[] to, String subject, String content, boolean html, String... filePaths) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
        messageHelper.setFrom(sender);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        if (html) content = htmlCompressor.compress(content);
        messageHelper.setText(content, html);
        // 添加附件
        for (String filePath : filePaths) {
            FileSystemResource fileResource = new FileSystemResource(new File(filePath));
            if (fileResource.exists()) {
                messageHelper.addAttachment(Objects.requireNonNull(fileResource.getFilename()), fileResource);
            }
        }
        mailSender.send(message);
        log.info("【{}】 - {} 发送成功！", subject, to);
        return content;
    }

    public String send(String to, String subject, String content, boolean html, String... filePaths) throws MessagingException {
        return send(new String[]{to}, subject, content, html, filePaths);
    }


    public String send(String to, String subject, String content) throws MessagingException {
        return send(to, subject, content, false);
    }

    public String send(String[] to, String subject, String content) throws MessagingException {
        return send(to, subject, content, false);
    }

    /**
     * 发送模板邮件
     *
     * @param to        邮件接收者
     * @param subject   邮件主题
     * @param paramMap  模板参数
     * @param template  模板
     * @param filePaths 文件路径列表
     * @throws MessagingException 邮件发送异常
     */
    public String send(String[] to, String subject, Map<String, Object> paramMap, String template, String... filePaths) throws MessagingException {
        Context context = new Context();
        context.setVariables(paramMap);
        String emailContent = templateEngine.process(template, context);
        return send(to, subject, emailContent, true, filePaths);
    }


    public String send(String to, String subject, Map<String, Object> paramMap, String template, String... filePaths) throws MessagingException {
        return send(new String[]{to}, subject, paramMap, template, filePaths);
    }
}
